package com.aliengen.thingple.thingple.api.models;

import com.aliengen.thingple.thingple.api.exception.FieldNotFoundException;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class Operator implements Serializable {
    public String firstname;
    public String lastname;
    public int operator_id;

    public Operator(){
    }

    public Operator(String firstname, String lastname, int operator_id)
    {
        this.firstname = firstname;
        this.lastname = lastname;
        this.operator_id = operator_id;
    }

    public Operator(JSONObject json) throws FieldNotFoundException
    {
        if(!(json.has("firstname") && json.has("lastname") && json.has("operator_id")))
        {
            throw new FieldNotFoundException("Missing fields!");
        }

        try {


            this.firstname = (String) json.get("firstname");
            this.lastname = (String) json.get("lastname");
            this.operator_id = Integer.parseInt((String) json.get("operator_id"));
        } catch(JSONException e)
        {
            throw new FieldNotFoundException(e);
        }
    }

    @Override
    public String toString()
    {
        return this.firstname + " " + this.lastname;
    }
}
